import React from "react";
import { type CveItem } from "~/server/api/routers/vulnerability";
import ReactMarkdown from "react-markdown";

interface RemediationProps {
  cve: CveItem;
}

const Remediation: React.FC<RemediationProps> = ({ cve }) => {
  // Extract remediation information from CVE data
  const hasOfficialFix = cve.cisaRequiredAction || cve.evaluatorSolution;

  // Find references that might contain remediation info
  const patchReferences =
    cve.references?.filter((ref) =>
      ref.tags?.some((tag) =>
        ["Patch", "Vendor Advisory", "Mitigation"].includes(tag)
      )
    ) || [];

  // Generate a remediation strategy based on CVE info
  const generateRemediation = () => {
    if (cve.evaluatorSolution) return cve.evaluatorSolution;
    if (cve.cisaRequiredAction) return cve.cisaRequiredAction;

    return (
      "No official remediation guidance is available for this vulnerability. Consider the following general steps:\n\n" +
      "1. Update affected software to the latest version if available\n" +
      "2. Apply vendor-provided patches when released\n" +
      "3. Consider implementing mitigating controls if patches are not available\n" +
      "4. Monitor vendor advisories for updated information"
    );
  };

  return (
    <div className="py-4">
      <div className="font-orbitron mb-4 text-xs font-bold uppercase tracking-wider text-violet-100 text-opacity-70">
        Remediation Guidance
      </div>

      <div className="grid gap-6 md:grid-cols-3">
        <div className="space-y-6 md:col-span-2">
          {/* Remediation Steps */}
          <div className="rounded-lg border border-gray-200 p-6 dark:border-gray-700">
            <h3 className="mb-4 text-lg font-medium text-gray-900 dark:text-white">
              Recommended Actions
            </h3>
            <div className="prose prose-sm dark:prose-invert max-w-none">
              <ReactMarkdown>{generateRemediation()}</ReactMarkdown>
            </div>

            {/* CISA Information if available */}
            {cve.cisaRequiredAction && (
              <div className="mt-4 rounded-md bg-blue-50 p-4 dark:bg-blue-900/20">
                <div className="flex">
                  <div className="flex-shrink-0">
                    <svg
                      className="h-5 w-5 text-blue-400"
                      xmlns="http://www.w3.org/2000/svg"
                      viewBox="0 0 20 20"
                      fill="currentColor"
                    >
                      <path
                        fillRule="evenodd"
                        d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z"
                        clipRule="evenodd"
                      />
                    </svg>
                  </div>
                  <div className="ml-3 flex-1">
                    <h3 className="text-sm font-medium text-blue-800 dark:text-blue-300">
                      CISA Required Action
                    </h3>
                    <div className="mt-2 text-sm text-blue-700 dark:text-blue-200">
                      <p>{cve.cisaRequiredAction}</p>
                      {cve.cisaActionDue && (
                        <p className="mt-2 font-medium">
                          Due Date: {cve.cisaActionDue}
                        </p>
                      )}
                    </div>
                  </div>
                </div>
              </div>
            )}
          </div>

          {/* Vendor Information */}
          {cve.vendorComments && cve.vendorComments.length > 0 && (
            <div className="rounded-lg border border-gray-200 p-6 dark:border-gray-700">
              <h3 className="mb-4 text-lg font-medium text-gray-900 dark:text-white">
                Vendor Comments
              </h3>
              {cve.vendorComments.map((comment, index) => (
                <div
                  key={index}
                  className="mb-4 border-b border-gray-100 pb-4 last:border-0 last:pb-0 dark:border-gray-800"
                >
                  <h4 className="font-medium text-gray-800 dark:text-gray-200">
                    {comment.organization}
                  </h4>
                  <p className="mt-1 text-sm text-gray-600 dark:text-gray-300">
                    {comment.comment}
                  </p>
                  <p className="mt-1 text-xs text-gray-500">
                    Last updated: {comment.lastModified}
                  </p>
                </div>
              ))}
            </div>
          )}
        </div>

        <div className="space-y-6">
          {/* Remediation Resources */}
          <div className="rounded-lg border border-gray-200 p-6 dark:border-gray-700">
            <h3 className="mb-4 text-lg font-medium text-gray-900 dark:text-white">
              Patch Resources
            </h3>
            {patchReferences.length > 0 ? (
              <ul className="space-y-3">
                {patchReferences.map((ref, index) => (
                  <li key={index} className="text-sm">
                    <a
                      href={ref.url}
                      target="_blank"
                      rel="noopener noreferrer"
                      className="text-violet-600 hover:text-violet-800 hover:underline dark:text-violet-400 dark:hover:text-violet-300"
                    >
                      {ref.url.replace(/^https?:\/\//, "").split("/")[0]}
                      {ref.tags && ref.tags.length > 0 && (
                        <span className="ml-2 text-xs text-gray-500">
                          [{ref.tags.join(", ")}]
                        </span>
                      )}
                    </a>
                  </li>
                ))}
              </ul>
            ) : (
              <p className="text-sm text-gray-500 dark:text-gray-400">
                No specific patch resources available.
              </p>
            )}
          </div>

          {/* Vulnerability Timeline */}
          <div className="rounded-lg border border-gray-200 p-6 dark:border-gray-700">
            <h3 className="mb-4 text-lg font-medium text-gray-900 dark:text-white">
              Timeline
            </h3>
            <div className="space-y-4">
              <div className="flex items-start">
                <div className="flex h-6 w-6 items-center justify-center rounded-full bg-violet-100 text-violet-800 dark:bg-violet-900/30 dark:text-violet-300">
                  <svg
                    xmlns="http://www.w3.org/2000/svg"
                    className="h-4 w-4"
                    viewBox="0 0 20 20"
                    fill="currentColor"
                  >
                    <path
                      fillRule="evenodd"
                      d="M6 2a1 1 0 00-1 1v1H4a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V6a2 2 0 00-2-2h-1V3a1 1 0 10-2 0v1H7V3a1 1 0 00-1-1zm0 5a1 1 0 000 2h8a1 1 0 100-2H6z"
                      clipRule="evenodd"
                    />
                  </svg>
                </div>
                <div className="ml-3">
                  <p className="text-sm font-medium text-gray-900 dark:text-white">
                    Published
                  </p>
                  <p className="text-sm text-gray-500 dark:text-gray-400">
                    {cve.published}
                  </p>
                </div>
              </div>
              <div className="flex items-start">
                <div className="flex h-6 w-6 items-center justify-center rounded-full bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300">
                  <svg
                    xmlns="http://www.w3.org/2000/svg"
                    className="h-4 w-4"
                    viewBox="0 0 20 20"
                    fill="currentColor"
                  >
                    <path
                      fillRule="evenodd"
                      d="M4 2a1 1 0 011 1v2.101a7.002 7.002 0 0111.601 2.566 1 1 0 11-1.885.666A5.002 5.002 0 005.999 7H9a1 1 0 010 2H4a1 1 0 01-1-1V3a1 1 0 011-1zm.008 9.057a1 1 0 011.276.61A5.002 5.002 0 0014.001 13H11a1 1 0 110-2h5a1 1 0 011 1v5a1 1 0 11-2 0v-2.101a7.002 7.002 0 01-11.601-2.566 1 1 0 01.61-1.276z"
                      clipRule="evenodd"
                    />
                  </svg>
                </div>
                <div className="ml-3">
                  <p className="text-sm font-medium text-gray-900 dark:text-white">
                    Last Modified
                  </p>
                  <p className="text-sm text-gray-500 dark:text-gray-400">
                    {cve.lastModified}
                  </p>
                </div>
              </div>
              {cve.cisaExploitAdd && (
                <div className="flex items-start">
                  <div className="flex h-6 w-6 items-center justify-center rounded-full bg-red-100 text-red-800 dark:bg-red-900/30 dark:text-red-300">
                    <svg
                      xmlns="http://www.w3.org/2000/svg"
                      className="h-4 w-4"
                      viewBox="0 0 20 20"
                      fill="currentColor"
                    >
                      <path
                        fillRule="evenodd"
                        d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z"
                        clipRule="evenodd"
                      />
                    </svg>
                  </div>
                  <div className="ml-3">
                    <p className="text-sm font-medium text-gray-900 dark:text-white">
                      Added to CISA KEV
                    </p>
                    <p className="text-sm text-gray-500 dark:text-gray-400">
                      {cve.cisaExploitAdd}
                    </p>
                  </div>
                </div>
              )}
            </div>
          </div>
        </div>
      </div>
    </div>
  );
};

export default Remediation;
